Skip to content

Conversation

@miguelmarcondesf
Copy link
Contributor

@miguelmarcondesf miguelmarcondesf commented Sep 27, 2025

feature: Easy associations between tenanted and untenanted models

enhance active record associations to detect if they're between a tenanted model and an untenanted model, and in that case do the extra work to make sure the tenant is respected.

Checklist:

  • has_one
  • has_many
  • belongs_to
  • Fixtures double check

@flavorjones
Copy link
Member

Hi! Thanks for opening this draft. I'll dig in and review as soon as I can find time!

@flavorjones
Copy link
Member

Sorry, it's been a busy week. I will definitely get to this soon!

Copy link
Member

@flavorjones flavorjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the right direction. I know in the original description I suggested something like .where(tenant_id: tenant) but that was meant to be illustrative.

The general problem that I personally need to solve is for a model where the tenant ID is not in the database.

@flavorjones
Copy link
Member

Hmm. Please ignore my review for now, I'm still wrapping my head around the problem and this approach.

Copy link
Member

@flavorjones flavorjones left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start on a very complex feature. I'm not at all sure how belongs_to might be implemented, but this seems like a promising direction for the has_one and has_many side of the association.

test "has_one automatically scopes by tenant_id" do
TenantedApplicationRecord.create_tenant("foo") do
user = User.create!(email: "[email protected]")
Announcement.create!(message: "Foo announcement", tenant_id: "foo", user: user)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally I'd like to be able to do this in an untenanted scope, but it looks like AR does some validation that requires a tenanted connection ...

I'd also like to be able to do this without explicitly setting the tenant id in Announcement, that is:

Announcement.create! user: user

should set the tenant column automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do we want to set this tenant column automatically even for untenanted ones?
For this specific case, User is tenanted by TenantedApplicationRecord and Annoucement is using SharedApplicationRecord
I can't find a simple way to manage the ActiveRecord call to the untenanted ones unless we transform them into Subtenanted ones 🤔

@miguelmarcondesf
Copy link
Contributor Author

This is a good start on a very complex feature. I'm not at all sure how belongs_to might be implemented, but this seems like a promising direction for the has_one and has_many side of the association.

@flavorjones Thanks for the awesome feedback, I'll try to get back to work here later this week!

@miguelmarcondesf miguelmarcondesf force-pushed the 201/enhanced-tenanted-untenanted-associations branch from 3895dd5 to be86ddc Compare October 18, 2025 15:19
@miguelmarcondesf
Copy link
Contributor Author

Hey @flavorjones
I made the changes you requested and added a belongs_to implementation following your suggestions.
Please, when you have time, I would be grateful for another review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants